package com.wanggy.mystore.config;

import com.wanggy.mystore.intreceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;

@Configuration //表示当前类是一个配置类，可以被springboot扫描到
public class LoginInterceptorConfigure implements WebMvcConfigurer {
    @Value("${upLoadFilePath}")
    private String upLoadFilePath;

    @Override
    //配置拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        //1.创建自定义的拦截器
        LoginInterceptor loginInterceptor = new LoginInterceptor();
        //2.配置白名单并存放在一个List集合中
        ArrayList<String> patterns = new ArrayList<>();
        patterns.add("/bootstrap3/**");
        patterns.add("/css/**");
        patterns.add("/images/**");
        patterns.add("/js/**");
        patterns.add("/web/register.html");
        patterns.add("/web/login.html");
        patterns.add("/web/index.html");
        patterns.add("/web/product.html");
        patterns.add("/users/reg");
        patterns.add("/users/login");
        patterns.add("/districts/**");
        patterns.add("/districts");
        //完成拦截器的注册，后面的addPathPatterns方法表示拦截所有请求，
        // excludePathPatterns表示排除的白名单中的请求
        registry.addInterceptor(loginInterceptor).
                addPathPatterns("/**").
                excludePathPatterns(patterns);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/upload/***")
                .addResourceLocations("file:"+upLoadFilePath);
    }
}
